package cn.sinjet.carassist;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import cn.sinjet.model.carassist.CarAssistService;
import cn.sinjet.model.carassist.INaviInfo;

public class MessageClient implements INaviInfo{
	final static String tag = "MsgClient";
	SinjetApplication mApp;
	Context mContext;
	private CarAssistService mLocalService = null;
    public MessageClient(SinjetApplication app){
    	mApp = app;
    }
    
    void init(Context context){
    	mContext = context;
        Intent intent = new Intent(mContext.getApplicationContext(),CarAssistService.class);
        mContext.startService(intent);
        if(mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)){
          	Log.d(tag, "bindService succeed!");
        }else{
        	Log.d(tag, "bindService fail!");
        }
        	
    }
    
    private ServiceConnection mConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName className) {
			// TODO Auto-generated method stub
			mLocalService = null;
			Log.d(tag, "onServiceDisconnected!"); 	
		}
		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			// TODO Auto-generated method stub
			Log.d(tag, "onServiceConnected!");
			mLocalService = ((CarAssistService.LocalBinder) service).getService();
		}
	};
	
	
    void uninit(){
    	
    }
    
    void sendMsgToService(Bundle data){
    	if(mLocalService != null){
    		Message msg = new Message();
    		msg.what = CarAssistService.MSG_NAVI_INFO;
    		msg.setData(data);
    		mLocalService.onRecvMessage(msg);
    	}
    }

    public void sendMsgToService(Message msg){
    	if(mLocalService != null){
    		mLocalService.onRecvMessage(msg);
    	}
    }
    
    public Handler getMsgHandler(){
    	if(mLocalService != null)
    		return mLocalService.getMsgHandler();
    	return null;
    }
    
	@Override
	public void onStepInfo(int iconType, int distance) {
		// TODO Auto-generated method stub
	   Bundle data = new Bundle();
	   data.putInt("what", INaviInfo.STEP_INFO);
	   data.putInt("iconType", iconType);
	   data.putInt("distance", distance);
	   sendMsgToService(data);
	}

	@Override
	public void onLineInfo(int retainDistance) {
		// TODO Auto-generated method stub
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.LINE_INFO);
	    data.putInt("retainDistance", retainDistance);
	    sendMsgToService(data);
	}


	@Override
	public void onLimitSpeed(int speed, int limitSpeed) {
		// TODO Auto-generated method stub
		Log.i("map", "limitSpeed:"+limitSpeed);
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.LIMIT_SPEED);
	    data.putInt("speed", speed);
	    data.putInt("limitSpeed", limitSpeed);
	    sendMsgToService(data);
	}

	@Override
	public void onCameraDistance(int distance, int dsp, int type) {
		// TODO Auto-generated method stub
		Log.i("map", "cameraDistance:"+distance+" display:"+dsp);
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.CAMERA_DISTANCE);
	    data.putInt("distance", distance);
	    data.putInt("dsp", dsp);
	    data.putInt("type", type);
	    sendMsgToService(data);
	}

	@Override
	public void onLaneInfo(int[] laneInfo) {
		// TODO Auto-generated method stub
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.LANE_INFO);
	    data.putIntArray("laneInfo", laneInfo);
	    sendMsgToService(data);
	}

	@Override
	public void onThirdPartyNavigating(boolean isNavigating) {
		// TODO Auto-generated method stub
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.BAIDU_NAVIGATING);
	    data.putBoolean("isNavigating", isNavigating);
	    sendMsgToService(data);		
	}

	@Override
	public void onAmapNavigating(boolean isNavigating) {
		// TODO Auto-generated method stub
		Bundle data = new Bundle();
	    data.putInt("what", INaviInfo.AMAP_NAVIGATING);
	    data.putBoolean("isNavigating", isNavigating);
	    sendMsgToService(data);
	}
    
    
}
